package tr.edu.yildiz.ce.dap.appagent.command.prompt;

import java.util.List;
import java.util.StringTokenizer;

import tr.edu.yildiz.ce.dap.appagent.command.deployment.agent.ListCommand;
import tr.edu.yildiz.ce.dap.appagent.command.deployment.agent.RetrieveCommand;
import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.AbstractAgentCommand;
import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.CommandConstants;
import tr.edu.yildiz.ce.dap.appagent.exception.BasicAgentException;
import tr.edu.yildiz.ce.dap.appagent.model.Component;
import tr.edu.yildiz.ce.dap.appagent.model.DapMessage;
import tr.edu.yildiz.ce.dap.appagent.process.base.AbstractProcess;

public class RetrievePromptCommand extends BasicPromptCommand {

	public RetrievePromptCommand(AbstractProcess process) {
		super(process);
	}

	@Override
	protected AbstractAgentCommand prepareAgentCommand(AbstractProcess process,
			String commandStr) throws BasicAgentException  {
		StringTokenizer sTokenizer = new StringTokenizer(commandStr," ");
		String command = sTokenizer.nextToken();
		String name = null;
		String version = null;
		while (sTokenizer.hasMoreElements()) {
			String pType = (String) sTokenizer.nextElement();
			if (pType.equals(CommandConstants.PARAMETER_TYPE.NAME)){
				name = sTokenizer.nextToken();
			}else if (pType.equals(CommandConstants.PARAMETER_TYPE.VERSION)){
				version = sTokenizer.nextToken(); 
			}
		}
		return new RetrieveCommand(process, name, version);
	}

	@Override
	protected void processResponse(DapMessage response) {
		List<Component> componentList = response.getComponentList().getComponent();
		for (Component component : componentList) {
			System.out.println(component.getName() + "(" + component.getVersion() + ")" + "is installed" );
		}
	}
}
